package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.BrandAddNewParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/brands")
@Api(tags = "02. 品牌管理模块")
@Validated
public class BrandController {

    @Autowired
    private IBrandService brandService;

    // http://localhost:9180/brands/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BrandAddNewParam brandAddNewParam) {
        log.debug("开始处理【新增品牌】的请求，参数：{}", brandAddNewParam);
        brandService.addNewBrand(brandAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/brands/{id}/delete
    @PostMapping("/{id:^\\d+$}/delete")
    @ApiOperation("根据ID删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "删除品牌失败，请提交合法的ID值！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据ID删除品牌】的请求，参数id：{}", id);
        brandService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/brands/{id}/enable
    @PostMapping("/{id:^\\d+$}/enable")
    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    public JsonResult enable(@Range(min = 1, message = "启用品牌失败，请提交合法的ID值！")
                             @PathVariable Long id) {
        log.debug("开始处理【启用品牌】的请求，参数id：{}", id);
        brandService.updateEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/brands/{id}/disable
    @PostMapping("/{id:^\\d+$}/disable")
    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    public JsonResult disable(@Range(min = 1, message = "禁用品牌失败，请提交合法的ID值！")
                             @PathVariable Long id) {
        log.debug("开始处理【禁用品牌】的请求，参数id：{}", id);
        brandService.updateDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/brands/{id}/update
    @PostMapping("/{id:^\\d+$}/update")
    @ApiOperation("根据ID修改品牌详情")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long"),
    })
    public JsonResult update(@PathVariable @Range(min = 1, message = "修改品牌失败，请提交合法的ID值！") Long id, BrandUpdateInfoParam brandUpdateInfoParam) {
        log.debug("开始处理【根据ID修改品牌详情】的请求，id：{},更新的数据", brandUpdateInfoParam);
        brandService.update(id, brandUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/brands/{id}
    @GetMapping("/{id:^\\d+$}")
    @ApiOperation("根据ID查询品牌详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long"),
    })
    public JsonResult select(@PathVariable @Range(min = 1, message = "查询品牌失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询品牌详情】的请求，参数id：{}", id);
        BrandStandardVO standard = brandService.getStandardById(id);
        return JsonResult.ok(standard);
    }

    // http://localhost:9180/brands
    @GetMapping("")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "删除品牌失败，请提交合法的ID值！") Integer page, String queryType) {
        log.debug("开始处理【根据ID查询品牌详情】的请求，页码：{}，参数类型：{}", page, queryType);
        page = page == null ? 1 : page;
        PageData<BrandListItemVO> pageData;

        if ("all".equals(queryType)) {
            pageData = brandService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = brandService.list(page);
        }
        return JsonResult.ok(pageData);
    }
}
